package com.hongyuan.venue.ui.home.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.hongyuan.venue.R;
import com.hongyuan.venue.data.md5.BaseUtil;
import com.hongyuan.venue.ui.home.beans.SecheduleBeans;
import com.hongyuan.venue.utils.TimeUtil;
import java.util.HashMap;
import java.util.Map;

import cn.zhouchaoyuan.excelpanel.BaseExcelPanelAdapter;

public class CustomAdapter extends BaseExcelPanelAdapter<SecheduleBeans.NameBean, Long, SecheduleBeans.ListBean> {

    private Context context;
    private View.OnClickListener blockListener;

    //改变高度
    final float scale;
    //记录一行有几列数据(第一个参数表示第几行,第二个参数表示高度倍数)
    private Map<Integer,Integer> multiple = new HashMap<>();
    //记录最大数据条目数
    private int maxNums;

    public CustomAdapter(Context context, View.OnClickListener blockListener) {
        super(context);
        this.context = context;
        this.blockListener = blockListener;
        scale = context.getResources().getDisplayMetrics().density;
    }

    public Map<Integer, Integer> getMultiple() {
        return multiple;
    }

    public void setMultiple(Map<Integer, Integer> multiple) {
        this.multiple = multiple;
    }

    public int getMaxNums() {
        return maxNums;
    }

    public void setMaxNums(int maxNums) {
        this.maxNums = maxNums;
    }

    //=========================================content's cell===========================================
    @Override
    public RecyclerView.ViewHolder onCreateCellViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_status_normal_cell, parent, false);
        CellHolder cellHolder = new CellHolder(layout);
        return cellHolder;
    }

    @Override
    public void onBindCellViewHolder(RecyclerView.ViewHolder holder, int verticalPosition, int horizontalPosition) {
        SecheduleBeans.ListBean cell = getMajorItem(verticalPosition, horizontalPosition);

        if (null == holder || !(holder instanceof CellHolder)) {
            return;
        }

        CellHolder viewHolder = (CellHolder) holder;
        viewHolder.tvContent.setTag(cell);
        viewHolder.dataContent.setTag(multiple.get(horizontalPosition));


        if(cell != null){
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)(74*scale+0.5f), (int)(42*multiple.get(horizontalPosition)*scale+0.5f));
            viewHolder.dataContent.setLayoutParams(layoutParams);
            //viewHolder.tvContent.setText(cell.getId());

            if (cell.getIs_reserve() == 1) {

                viewHolder.tvContent.setClickable(true);
                viewHolder.tvContent.setOnClickListener(blockListener);

                viewHolder.tvContent.setText("¥"+ BaseUtil.getNoZoon(cell.getType_price()));
                if(cell.isSelect()){
                    viewHolder.tvContent.setTextColor(ContextCompat.getColor(context,R.color.theme_FFFFFF));
                    viewHolder.tvContent.setBackgroundColor(ContextCompat.getColor(context, R.color.theme_23C095));
                }else{
                    viewHolder.tvContent.setTextColor(ContextCompat.getColor(context,R.color.theme_333333));
                    viewHolder.tvContent.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
                }
            } else{
                viewHolder.tvContent.setText("不可用");
                viewHolder.tvContent.setTextColor(ContextCompat.getColor(context,R.color.theme_FFFFFF));
                viewHolder.tvContent.setBackgroundColor(ContextCompat.getColor(context, R.color.theme_CCCCCC));
                viewHolder.tvContent.setClickable(false);
            }
        }else{
            if(multiple.get(horizontalPosition) != 1){

                 if(verticalPosition < maxNums/multiple.get(horizontalPosition)){
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)(74*scale+0.5f), (int)(42*multiple.get(horizontalPosition)*scale+0.5f));
                    viewHolder.dataContent.setLayoutParams(layoutParams);

                     viewHolder.tvContent.setText("不可用");
                     viewHolder.tvContent.setTextColor(ContextCompat.getColor(context,R.color.theme_FFFFFF));
                     viewHolder.tvContent.setBackgroundColor(ContextCompat.getColor(context, R.color.theme_CCCCCC));
                     viewHolder.tvContent.setClickable(false);
                }else{
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)(74*scale+0.5f), 0);
                    viewHolder.dataContent.setLayoutParams(layoutParams);
                }

            }else{
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)(74*scale+0.5f), 0);
                viewHolder.dataContent.setLayoutParams(layoutParams);
            }
        }

    }

    static class CellHolder extends RecyclerView.ViewHolder {

        public final TextView tvContent;
        public final RelativeLayout dataContent;

        public CellHolder(View itemView) {
            super(itemView);
            dataContent = itemView.findViewById(R.id.dataContent);
            tvContent = itemView.findViewById(R.id.tvContent);
        }
    }


    //=========================================top cell===========================================
    @Override
    public RecyclerView.ViewHolder onCreateTopViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_status_top_header_item, parent, false);
        TopHolder topHolder = new TopHolder(layout);
        return topHolder;
    }

    @Override
    public void onBindTopViewHolder(RecyclerView.ViewHolder holder, int position) {
        SecheduleBeans.NameBean rowTitle = getTopItem(position);
        if (null == holder || !(holder instanceof TopHolder) || rowTitle == null) {
            return;
        }
        TopHolder viewHolder = (TopHolder) holder;
        viewHolder.topContent.setText(rowTitle.getArea_name());
    }

    static class TopHolder extends RecyclerView.ViewHolder {

        public final TextView topContent;

        public TopHolder(View itemView) {
            super(itemView);
            topContent = itemView.findViewById(R.id.topContent);
        }
    }

    //=========================================left cell===========================================
    @Override
    public RecyclerView.ViewHolder onCreateLeftViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_status_left_header_item, parent, false);
        LeftHolder leftHolder = new LeftHolder(layout);
        return leftHolder;
    }

    @Override
    public void onBindLeftViewHolder(RecyclerView.ViewHolder holder, int position) {
        Long colTitle = getLeftItem(position);
        if (null == holder || !(holder instanceof LeftHolder) || colTitle == null) {
            return;
        }
        LeftHolder viewHolder = (LeftHolder) holder;
        viewHolder.tvLeft.setText(TimeUtil.getStringByFormat(colTitle,TimeUtil.dateFormatHM));
        ViewGroup.LayoutParams lp = viewHolder.leftBox.getLayoutParams();
        viewHolder.leftBox.setLayoutParams(lp);
        viewHolder.leftBox.setTag(1);
    }

    static class LeftHolder extends RecyclerView.ViewHolder {

        public final TextView tvLeft;
        public final RelativeLayout leftBox;

        public LeftHolder(View itemView) {
            super(itemView);
            tvLeft = itemView.findViewById(R.id.tvLeft);
            leftBox = itemView.findViewById(R.id.leftBox);
        }
    }

    //=========================================left-top cell===========================================
    @Override
    public View onCreateTopLeftView() {
        return LayoutInflater.from(context).inflate(R.layout.room_status_left_top, null);
    }
}
